<template>
	<div class="app-container">
		<el-form class="queryForms" :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
			label-width="70px">
			<el-form-item label="产品名称" prop="productName">
				<el-input maxlength="20" show-word-limit v-model="queryParams.productName" placeholder="请输入产品名称"
					clearable @keyup.enter.native="handleQuery" />
			</el-form-item>
			<el-form-item label="返修任务单" prop="repairNumber" label-width="90px">
				<el-input maxlength="20" show-word-limit v-model="queryParams.repairNumber" placeholder="请输入返修任务单"
					clearable @keyup.enter.native="handleQuery" />
			</el-form-item>
			<el-form-item label="产品编号" prop="productNumber">
				<el-input maxlength="20" show-word-limit v-model="queryParams.productNumber" placeholder="请输入产品编号"
					clearable @keyup.enter.native="handleQuery" />
			</el-form-item>
			<el-form-item label="3B号" prop="bNumber">
				<el-input maxlength="20" show-word-limit v-model="queryParams.bNumber" placeholder="请输入3B号" clearable
					@keyup.enter.native="handleQuery" />
			</el-form-item>
			<el-form-item>
				<el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
				<el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>
		<el-row :gutter="10" class="mb8">
			<!-- <el-col :span="1.5">
				<el-button type="primary" size="mini" :disabled="multiple" @click="handleUpdates"
					v-hasPermi="['erp:quality:editcheck']">质检</el-button>
			</el-col> -->
			<right-toolbar menuNames="quality36" @changeTableData="changeTableData" :showSearch.sync="showSearch"
				@queryTable="getList" :columns.sync="columns" perm="erp:quality:list"></right-toolbar>
		</el-row>

		<div class="informationTab" ref="eleTab">
			<el-table ref="table" @row-click="row_click" v-loading="loading" :data="repairProductProcessList"
				@selection-change="handleSelectionChange" border height="100%">
				<el-table-column type="selection" width="55" align="center" />
				<el-table-column label="序号" align="center" type="index" width="55" />
				<el-table-column label="返修任务单" prop="repairNumber" min-width="150" :show-overflow-tooltip="true"
					v-if="columns[0].visible" />
				<el-table-column label="产品名称" prop="productName" min-width="150" :show-overflow-tooltip="true"
					v-if="columns[1].visible" />
				<el-table-column label="产品型号" prop="productModel" min-width="150" :show-overflow-tooltip="true"
					v-if="columns[2].visible" />
				<el-table-column label="产品编号" prop="productNumber" min-width="150" :show-overflow-tooltip="true"
					v-if="columns[3].visible" />
				<el-table-column label="3B号" prop="bNumber" min-width="150" :show-overflow-tooltip="true"
					v-if="columns[4].visible" />
				<el-table-column label="维修类别" prop="maintenanceCategoryName" min-width="150"
					:show-overflow-tooltip="true" v-if="columns[5].visible" />
				<!-- <el-table-column label="故障原因" prop="failureCause" min-width="150" :show-overflow-tooltip="true" v-if="columns[6].visible" /> -->
				<el-table-column label="故障模式" prop="failureMode" min-width="150" :show-overflow-tooltip="true"
					v-if="columns[6].visible" />
				<el-table-column label="故障核实情况" align="center" prop="malfunctionStatus" min-width="150"
					:show-overflow-tooltip="true" v-if="columns[7].visible">
					<template slot-scope="scope">
						<div v-if="scope.row.malfunctionStatus == '0'">无故</div>
						<div v-if="scope.row.malfunctionStatus == '1'">有故</div>
						<div v-if="scope.row.malfunctionStatus == '2'">待核实</div>
						<div v-if="scope.row.malfunctionStatus == '3'">正常</div>
					</template>
				</el-table-column>
				<el-table-column label="工艺通知单编号" prop="processNumber" min-width="150" :show-overflow-tooltip="true"
					v-if="columns[8].visible" />
				<el-table-column label="质检状态" align="center" prop="qualityStatus" min-width="150"
					:show-overflow-tooltip="true" v-if="columns[9].visible">
					<template slot-scope="scope">
						<el-tag type="warning" v-if="scope.row.qualityStatus == '0'">待质检</el-tag>
						<el-tag v-if="scope.row.qualityStatus == '2'">质检中</el-tag>
						<el-tag type="success" v-if="scope.row.qualityStatus == '3'">已完成</el-tag>
					</template>
				</el-table-column>
				<el-table-column label="操作" class-name="small-padding fixed-width" fixed="right" width="100"
					align="center">
					<template slot-scope="scope">
						<el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
							v-hasPermi="['erp:qualityInspection:edit']">质检工序</el-button>
					</template>
				</el-table-column>
			</el-table>
		</div>
		<pagination menuNames="Pagination39" v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
			:limit.sync="queryParams.pageSize" @pagination="getList" />

		<el-dialog title="质检" :visible.sync="openQuality" width="900px" append-to-body v-dialogDrag>
			<el-form ref="forms" :model="forms" label-width="80px">
				<el-form-item label="质检意见" prop="qualityTestOpinion">
					<el-input v-model="forms.qualityTestOpinion" placeholder="请输入质检意见" type="textarea" maxlength="100"
						show-word-limit />
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="submitForm">合 格</el-button>
				<el-button @click="cancel">不 合 格</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
import { productList1, getRepairProductProcess, qualityBatch } from "@/api/taskFilling/repairProductProcess"
import { mapGetters } from "vuex"
import { getcolumn } from "@/api/selectList"
import searchStateMixin from '@/layout/mixin/searchStateMixin.js'
export default {
	name: "Quality_menu",
	mixins: [searchStateMixin],
	computed: {
		...mapGetters(["sidebar"])
	},
	data() {
		return {
			openQuality: false,
			singleIds: true,
			eleTabTop: 0, //表格容器距浏览器顶部距离
			processStatusList: [
				{
					id: "0",
					name: "未完工"
				},
				{
					id: "1",
					name: "已完工"
				},
				{
					id: "2",
					name: "质检通过"
				},
				{
					id: "3",
					name: "质检不通过"
				}
			],
			// 遮罩层
			loading: true,
			// 选中数组
			ids: [],
			// 非单个禁用
			single: true,
			// 非多个禁用
			multiple: true,
			// 显示搜索条件
			showSearch: true,
			// 总条数
			total: 0,
			// 返修单产品工序列表格数据
			repairProductProcessList: [],
			// 弹出层标题
			title: "",
			// 是否显示弹出层
			open: false,
			// 查询参数
			queryParams: {
				pageNum: 1,
				pageSize: 10,
				repairProductId: null,
				productProcessId: null,
				productTechnologyId: null,
				productTechnologyName: null,
				processCode: null,
				processName: null,
				productName: null,
				productModel: null,
				processTime: null,
				content: null,
				processStatus: null,
				accomplishUserId: null,
				accomplishUserName: null,
				repairNumber: null,
				productNumber: null,
				bNumber: null,
				malfunctionStatus: null
			},
			// 表单参数
			form: {},
			// 表单校验
			rules: {
				delFlag: [{ required: true, message: "删除标志不能为空", trigger: "blur" }]
			},
			// 列信息
			columns: [
				{ key: 0, label: `返修任务单`, visible: true },
				{ key: 1, label: `产品名称`, visible: true },
				{ key: 2, label: `产品型号`, visible: true },
				{ key: 3, label: `产品编号`, visible: true },
				{ key: 4, label: `3B号`, visible: true },
				{ key: 5, label: `维修类别`, visible: true },
				// { key: 6, label: `故障原因`, visible: true },
				{ key: 6, label: `故障模式`, visible: true },
				{ key: 7, label: `故障核实情况`, visible: true },
				{ key: 8, label: `工艺通知单编号`, visible: true },
				{ key: 9, label: `质检状态`, visible: true }
			],
			forms: {}
		}
	},
	created() {
		// this.getSize()
		// 如果是页面刷新，重置查询
		if (window.performance && performance.navigation.type === performance.navigation.TYPE_RELOAD) {
			// 清除URL中的查询参数（不刷新页面）
			this.resetForm("queryForm")
			window.history.replaceState({}, document.title, window.location.pathname)
		}
	},
	mounted() {
		// 滚动条的获取
		this.handleScrollx()
		window.addEventListener("scroll", this.handleScrollx, true)
	},
	watch: {
		"$route.query": {
			handler(newQuery, oldQuery) {
				if (newQuery.queryData) {
					if (Object.keys(newQuery.queryData).length === 0) {
						this.resetQuerys()
						// this.getSize()
					} else {
						this.queryParams = { ...this.queryParams, ...JSON.parse(newQuery.queryData) }
						// this.getSize()
					}
				}  this.getSize()
			},
			immediate: true, // 立即执行一次
			deep: true // 深度监听
		}
	},
	activated() {
		// this.getSize()
		//获取表格容器距页面头部的距离
		this.handleScrollx()
	},
	destroyed() {
		window.removeEventListener("scroll", this.handleScrollx, true)
	},
	methods: {
		/** 重置按钮操作 */
		resetQuerys() {
			this.resetForm("queryForm")
				; (this.queryParams = {
					pageNum: 1,
					pageSize: this.queryParams.pageSize,
					repairProductId: null,
					productProcessId: null,
					productTechnologyId: null,
					productTechnologyName: null,
					processCode: null,
					processName: null,
					productName: null,
					productModel: null,
					processTime: null,
					content: null,
					processStatus: null,
					accomplishUserId: null,
					accomplishUserName: null,
					repairNumber: null,
					productNumber: null,
					bNumber: null,
					malfunctionStatus: null
				})
		},
		getSize() {
			getcolumn({ pageName: "Pagination39" }).then(response => {
				this.queryParams.pageSize = response.data.length ? (response.data[0].limit ? response.data[0].limit : 10) : 10
				this.getList()
			})
		},
		// 单击表格行
		row_click(row, column, event) {
			if (column && column.label == "操作") {
				return
			} else {
				this.$refs.table.toggleRowSelection(row)
			}
		},
		changeTableData() {
			this.$nextTick(() => {
				this.$refs.table.doLayout()
			})
		},

		setStatusData(ststus) {
			const loading = this.$loading({
				lock: true,
				text: "Loading",
				spinner: "el-icon-loading",
				background: "rgba(0, 0, 0, 0.7)"
			})
			qualityBatch({
				ids: this.ids,
				submitStatus: ststus,
				qualityTestOpinion: this.forms.qualityTestOpinion
			}).then(res => {
				this.$modal.msgSuccess("设置成功")
				localStorage.setItem("todoMessage", "quality_menu")
				this.openQuality = false
				loading.close()
				this.getList()
			}).finally(res => {
				loading.close()
			})
		},
		// 取消按钮
		cancel() {
			this.setStatusData("6")
		},
		submitForm() {
			this.setStatusData("5")
		},
		resetquality() {
			this.forms = {
				qualityTestOpinion: undefined
			}
		},
		/** 修改按钮操作 */
		handleUpdates() {
			this.resetquality()
			// 质检
			this.openQuality = true
		},
		//获取表格容器距页面头部的距离
		handleScrollx() {
			this.eleTabTop = this.$refs.eleTab.getBoundingClientRect().top
		},
		handleDetail(row) {
			getRepairProductProcess(row.id).then(res => {
				this.form = res.data
				this.open = true
			})
		},
		handleFiles(row) {
			this.$router.push({
				path: "/attachment/attachmentUpload",
				query: {
					TableId: row.id,
					typeId: 15,
					perm: "erp:taskFilling:files",
					// queryParams: JSON.stringify(this.queryParams),
					paths: "/quality/quality_menu"
				}
			})
		},
		/** 查询返修单产品工序列列表 */
		getList() {
			this.loading = true
			productList1(this.queryParams).then(response => {
				this.repairProductProcessList = response.rows
				this.total = response.total
				this.loading = false
			})
		},
		// 取消按钮
		cancel() {
			this.open = false
			this.reset()
		},
		// 表单重置
		reset() {
			this.form = {
				id: null,
				repairProductId: null,
				productProcessId: null,
				productTechnologyId: null,
				productTechnologyName: null,
				processCode: null,
				processName: null,
				productName: null,
				productModel: null,
				processTime: null,
				content: null,
				processStatus: null,
				accomplishUserId: null,
				accomplishUserName: null,
				remark: null,
				repairNumber: null,
				updateTime: null
			}
			this.resetForm("form")
		},
		/** 搜索按钮操作 */
		handleQuery() {
			this.queryParams.pageNum = 1
			this.saveSearchState()
			this.getList()
		},
		/** 重置按钮操作 */
		resetQuery() {
			this.resetForm("queryForm")
				; (this.queryParams = {
					pageNum: 1,
					pageSize: this.queryParams.pageSize,
					repairProductId: null,
					productProcessId: null,
					productTechnologyId: null,
					productTechnologyName: null,
					processCode: null,
					processName: null,
					productName: null,
					productModel: null,
					processTime: null,
					content: null,
					processStatus: null,
					accomplishUserId: null,
					accomplishUserName: null,
					repairNumber: null,
					productNumber: null,
					bNumber: null,
					malfunctionStatus: null
				}),
				this.clearSearchCache()
					this.handleQuery()
		},
		// 多选框选中数据
		handleSelectionChange(selection) {
			this.ids = selection.map(item => item.id)
			this.single = selection.length !== 1
			this.multiple = !selection.length
		},
		/** 新增按钮操作 */
		handleAdd() {
			this.reset()
			this.open = true
			this.title = "添加返修单产品工序列"
		},
		/** 修改按钮操作 */
		handleUpdate(row) {
			// status   1修改   2 填写
			this.$router.push({
				path: "/quality/qualityInspections/qualityInspection/index",
				query: {
					id: row.id,
					productName: row.productName,
					productModel: row.productModel,
					productNumber: row.productNumber,
					// queryParams: JSON.stringify(this.queryParams),
					paths: "/quality/quality_menu"
				}
			})
		}
	}
}
</script>
<style lang="scss" scoped>
::v-deep .el-table__fixed-header-wrapper .fixed-width {
	width: 100px !important;
}
</style>
